import requests

class ApiClient:
    # 初始化
    def __init__(self,base_url):
        self.base_url = base_url.rstrip('/')
        self.session = requests.Session()
    # 网址域名
    def url(self,path):
        return f'{self.base_url}/{path.lstrip("/")}'

    # 请求方式GET，post，put，delete
    def get(self,path,json_data,params=None,data=None,headers=None):
        return self.session.get(self.url(path),params=params,headers=headers)

    def post(self,path,json_data,params=None,data=None,headers=None):
        kwargs = {'json':json_data if json_data else {'data':data}}
        if kwargs:
            kwargs['params'] = params
        return self.session.post(self.url(path),headers=headers,**kwargs)
    def put(self, path, json_data, params=None, data=None, headers=None):
        kwargs = {'json': json_data if json_data else {'data': data}}
        if kwargs:
            kwargs['params'] = params
        return self.session.put(self.url(path), headers=headers, **kwargs)

    def delete(self, path, json_data, params=None, data=None, headers=None):
        return self.session.delete(self.url(path), params=params, headers=headers)
